package com.gxa.saas.admin.controller;

import com.gxa.saas.admin.config.UploadConfig;
import com.gxa.saas.admin.service.BannerService;
import com.gxa.saas.core.common.JsonResult;
import com.gxa.saas.core.entity.vo.BannerVO;
import com.gxa.saas.core.entity.vo.CommodityVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author cowboy
 * @Date 2022/7/19 21:17
 * @Version 1.0
 */
@Api("Banner管理模块")
@RestController
@Validated
public class BannerController {

    @Autowired
    private BannerService bannerService;

    /**
     *
     * @param file
     * @return java.util.Map
     * @author cowboy
     * @date 2022/7/27 10:48
     */
    @ApiOperation("图片上传")
    @PostMapping("/BannerPic")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "form",dataType="__file", name = "picture",value = "照片", required = true)})
    public Map BannerPic(@RequestPart("file") MultipartFile file) throws IOException {
        String url = UploadConfig.upload(file);
        System.out.println(url);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("图片上传成功");
        jsonResult.setData(url);
        return jsonResult.getMap();
    }

    /**
     * 无参查询
     * @param
     * @return java.util.Map
     * @author cowboy
     * @date 2022/7/19 21:31
     */
    @ApiOperation("无参查询")
    @PostMapping("/QueryBanner")
    public Map QueryBanner(){
        BannerVO bannerVO = this.bannerService.QueryBanner();
        JsonResult jsonResult = new JsonResult();

        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(bannerVO);
        return jsonResult.getMap();
    }

    /**
     * 条件查询,根据名称查询
     * @param name
     * @return java.util.Map
     * @author cowboy
     * @date 2022/7/19 21:31
     */
    @ApiOperation("条件查询")
    @PostMapping("/QueryBannerByCondition")
    public Map QueryBannerByCondition(@RequestParam(value = "name",required = true)@NotNull String name){
        BannerVO bannerVO = this.bannerService.QueryBannerByCondition(name);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(bannerVO);
        return jsonResult.getMap();
    }

    /**
     * 添加
     * @param bannerVO
     * @return java.util.Map
     * @author cowboy
     * @date 2022/7/19 21:32
     */
    @ApiOperation("添加Banner")
    @PostMapping("/InsertBanner")
    public Map InsertBanner(@ModelAttribute @NotNull BannerVO bannerVO){
        this.bannerService.InsertBanner(bannerVO);
        System.out.println(bannerVO);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("添加成功");
        return jsonResult.getMap();
    }


    /**
     * 编辑
     * @param bannerVO
     * @return java.util.Map
     * @author cowboy
     * @date 2022/7/19 21:32
     */
    @ApiOperation("编辑")
    @PostMapping("/OperationBanner")
    public Map OperationBanner(@ModelAttribute @NotNull BannerVO bannerVO) {
        this.bannerService.OperationBanner(bannerVO);
        JsonResult jsonResult = new JsonResult();

        jsonResult.setCode("1");
        jsonResult.setMsg("修改成功");

        return jsonResult.getMap();
    }


    /**
     * 删除
     * @param id
     * @return java.util.Map
     * @author cowboy
     * @date 2022/7/19 21:32
     */
    @ApiOperation("删除")
    @PostMapping("/DeleteBanner")
    public Map DeleteBanner(@RequestParam(value = "id",required = true)@NotNull Integer id) {
        this.bannerService.DeleteBanner(id);
        JsonResult jsonResult = new JsonResult();

        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功");

        return jsonResult.getMap();
    }

    /**
     * 添加/编辑banner无参查询商品
     * @param
     * @return java.util.Map
     * @author cowboy
     * @date 2022/7/27 18:01
     */
    @ApiOperation("添加＆编辑banner无参查询商品")
    @PostMapping("/BannerQueryProduct")
    public Map QueryProduct(){
        List<CommodityVO> commodityVO = this.bannerService.QueryProduct();
        JsonResult jsonResult = new JsonResult();

        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(commodityVO);

        return jsonResult.getMap();
    }

    /**
     *
     * @param name
     * @return java.util.Map
     * @author cowboy
     * @date 2022/7/27 18:29
     */
    @ApiOperation("添加＆编辑banner条件查询商品")
    @PostMapping("/QueryProductByName")
    public Map QueryProductByName(@RequestParam(value = "name",required = true)String name){
        List<CommodityVO> commodityVO = this.bannerService.QueryProductByName(name);
        JsonResult jsonResult = new JsonResult();

        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(commodityVO);

        return jsonResult.getMap();
    }

    /**
     *
     * @param id
     * @return java.util.Map
     * @author cowboy
     * @date 2022/7/27 18:37
     */
    @ApiOperation("编辑banner时根据banner的Id删除商品")
    @PostMapping("/DeleteProduct")
    public Map DeleteProduct(@RequestParam(value = "id",required = true)Integer id){
        this.bannerService.DeleteProduct(id);
        JsonResult jsonResult = new JsonResult();

        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功");

        return jsonResult.getMap();
    }
}
